// AchievementServiceImpl.java
package com.ts.service.achievement.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ts.dto.AchievementPageDTO;
import com.ts.entity.Achievement;
import com.ts.mapper.achievement.AchievementMapper;
import com.ts.result.PageResult;
import com.ts.service.achievement.AchievementService;
import com.ts.vo.AchievementVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class AchievementServiceImpl implements AchievementService {

    @Autowired
    private AchievementMapper achievementMapper;

    @Override
    public PageResult pageQuery(AchievementPageDTO dto) {
        // 设置分页参数
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());

        // 执行查询
        List<Achievement> achievements = achievementMapper.selectByCondition(dto);

        // 转换为VO
        List<AchievementVO> achievementVOS = achievements.stream().map(achievement -> {
            AchievementVO vo = new AchievementVO();
            BeanUtils.copyProperties(achievement, vo);
            return vo;
        }).collect(Collectors.toList());

        // 获取分页信息
        PageInfo<Achievement> pageInfo = new PageInfo<>(achievements);

        // 返回分页结果
        return new PageResult(pageInfo.getTotal(), achievementVOS);
    }
}